import { Metadata } from "next";
import { CoursePreview } from "~/components/courses/preview/CoursePreview";
import { createServerClient } from "~/trpc/server-client";
import { notFound } from "next/navigation";

interface Props {
  params: { courseId: string };
}

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const trpc = await createServerClient();
  const course = await trpc.courses.getById.fetch({ 
    courseId: params.courseId 
  });
  
  if (!course) {
    return {
      title: "课程不存在 - 在线教育平台",
    };
  }

  return {
    title: `${course.title} - 预览 - 在线教育平台`,
    description: course.description || undefined,
  };
}

export default async function CoursePreviewPage({ params }: Props) {
  const trpc = await createServerClient();
  
  try {
    // 验证课程是否存在
    const course = await trpc.courses.getById.fetch({ 
      courseId: params.courseId 
    });

    if (!course) {
      notFound();
    }

    return (
      <div className="h-screen flex">
        <CoursePreview 
          courseId={course.id} 
          initialCourse={course}
        />
      </div>
    );
  } catch (error) {
    notFound();
  }
} 